<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>SSE</title>
</head>
<body>
<input id="input" placeholder="请输入对话内容！">
<button id="btn">提交</button>

<script>
    const input = document.querySelector("#input")
    const btn = document.querySelector("#btn")


    btn.onclick = () => {
        if (!window.EventSource) {
            alert(("你的浏览器不支持SSE~"))
            return
        }

        // 根据环境的不同，变更url
        const url = `http://127.0.0.1:8080/api/ai/article?content=${input.value}&token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySUQiOjYsInVzZXJuYW1lIjoiZXdxIiwicm9sZSI6MSwiZXhwIjoxNzM5MjQwNDkwLCJpc3MiOiJqaWVnZSJ9.SOWLXnujwLG3oSkYhHPxIJa1mNMeiMJOOzpr98suDwc`

        const eventSource = new EventSource(url);

        eventSource.onmessage = (e) => {
            const message = JSON.parse(e.data);
            //this.messages.push(message);
            console.log("已接受到消息:",message)
        };
        eventSource.onerror = (event) => {
            console.log(e)
        };
        //处理服务器响应报文中的自定义事件
        eventSource.addEventListener("close", function (e) {
            console.log(e)
        });
    }


</script>
</body>
</html>